package com.yylending.las.authority.rsa;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yylending.las.Global;

import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

public class RsaModel extends Model {
	private static final String TAG = RsaModel.class.getSimpleName();

	protected static final int MSG_GET_RSA = 1;

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		BaseResultEntity result = cast(obj);
		switch (result.requestCode) {
			case MSG_GET_RSA:
				RSAResult rsa = cast(obj);
				EventAgent.post(rsa);
				break;
		}
	}

	@Override
	public Observable getObservable(Object obj, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = Global.LOGIN_TOKEN;
		switch (functionalCode) {
			case MSG_GET_RSA:
				return service.getRsaKey(token, new EmptyEntity());
		}
		return null;
	}

	private interface Service {
		/**
		 * 更新RSA密钥
		 */
		@POST("server/security/getRSAKey")
		Observable<RSAResult> getRsaKey(@Header("token") String token, @Body EmptyEntity params);
	}

	static class RSAResult extends BaseResultEntity<RsaBean> {
	}

	static class RsaBean {
		public String key;
	}

}
